home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8570 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  115 lines

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Fortran to C conversion help?
  5. Date: 4 Mar 1996 19:15:03 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4hgbjnINN136@keats.ugrad.cs.ubc.ca>
  8. References: <4hf04k$ogs@pipe3.nyc.pipeline.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4hf04k$ogs@pipe3.nyc.pipeline.com>,
  12. Doug Kolmar <dkolmar@grovestocktn.com> wrote:
  13. >Hello folks, 
  14. >I'm sort of a weekend C programmer so perhaps I've missed something, but
  15. >I'm trying to convert a function in FORTRAN to C. 
  16. >Attached is the FORTRAN code followed by the C code that I've written. The
  17. >C program will compile and run, but does not yield the correct output. I.E.
  18. >integers in the range 0 to 31. 
  19. >Except for the differences in the range of the RAND functions, I know
  20. >nothing about FORTRAN, so am I missing something implicit in the code? Any
  21. >help would be greatly appreciated. 
  22.  
  23. Use an automatic translator. Here is the output of f2c:
  24.  
  25. First of all, I had to fix up your fortran program. Fortran requires that the
  26. first six characters of each punched card^H^H^H^H^H^H^H^H^H^H^H^Hsource line
  27. be blank, or contain a line number or 'C' character for comment. Secondly, you
  28. are missing a line 20, the target of the GO TO statement. I just stuck in a
  29. line 20 wherever I liked:
  30.  
  31. file fortran.f:
  32.  
  33.  
  34.       FUNCTION I1OVRF(LAST) 
  35. C     THE ARGUMENT, LAST, IS THE PREVIOUS VALUE OF THE SEQUENCE 
  36.       NEW=0 
  37. C     THE VARIABLE K EQUALS ONE-HALF THE NUMBER OF POSSIBLE VALUES 
  38.       K=16 
  39.       L=LAST 
  40. C     THE VARIABLE PROBIT EQUALS 1/(NUMBER OF POSSIBLE VALUES) 
  41.       PROBIT = .03125 
  42. 20    J=L/K 
  43.       IF (J.EQ.1) L=L-K 
  44.       U=RAND(0) 
  45.       IF (U.LT.PROBIT) J=1-J 
  46.       NEW = NEW+J*K 
  47.       K=K/2 
  48.       PROBIT=PROBIT*2 
  49.       IF (K.GE.1) GO TO 20 
  50.       I1OVRF=NEW 
  51.       RETURN 
  52.       END  
  53.  
  54. file fortran.c produced by AT&T's f2c:
  55.  
  56.  
  57. /* fortran.f -- translated by f2c (version 19940305).
  58.    You must link the resulting object file with the libraries:
  59.     -lf2c -lm   (in that order)
  60. */
  61.  
  62. #include "f2c.h"
  63.  
  64. /* Table of constant values */
  65.  
  66. static integer c__0 = 0;
  67.  
  68. integer i1ovrf_(last)
  69. integer *last;
  70. {
  71.     /* System generated locals */
  72.     integer ret_val;
  73.  
  74.     /* Local variables */
  75.     extern doublereal rand_();
  76.     static integer j, k, l;
  77.     static real u, probit;
  78.     static integer new_;
  79.  
  80. /*     THE ARGUMENT, LAST, IS THE PREVIOUS VALUE OF THE SEQUENCE */
  81.     new_ = 0;
  82. /*     THE VARIABLE K EQUALS ONE-HALF THE NUMBER OF POSSIBLE VALUES */
  83.     k = 16;
  84.     l = *last;
  85. /*     THE VARIABLE PROBIT EQUALS 1/(NUMBER OF POSSIBLE VALUES) */
  86.     probit = (float).03125;
  87. L20:
  88.     j = l / k;
  89.     if (j == 1) {
  90.     l -= k;
  91.     }
  92.     u = rand_(&c__0);
  93.     if (u < probit) {
  94.     j = 1 - j;
  95.     }
  96.     new_ += j * k;
  97.     k /= 2;
  98.     probit *= 2;
  99.     if (k >= 1) {
  100.     goto L20;
  101.     }
  102.     ret_val = new_;
  103.     return ret_val;
  104. } /* i1ovrf_ */
  105.  
  106.  
  107. This is one of those rare occurances when the output of a translator is more
  108. readable than the source.
  109. -- 
  110.  
  111.